package com.tc.dispatch.web.util;


import java.text.DecimalFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class FloatJsonValueProcessor implements JsonValueProcessor {
    private static final Log log = LogFactory.getLog(FloatJsonValueProcessor.class);

    private String format = "######.0000000";

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public Object processArrayValue(Object value, JsonConfig jsonConfig) {

        if (value == null) {
            return value;
        }

        Float[] floats = (Float[]) value;
        String[] resultValue = new String[floats.length];

        DecimalFormat sf = new DecimalFormat(format);
        for (int i = 0; i < floats.length; i++) {
            resultValue[i] = sf.format(floats[i]);
        }
        return resultValue;
    }

    public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
        
    	//System.out.println("key:"+key+"_____value:"+value.toString());
        if (value == null) {
            return value;
        }
        Float fl=(Float)value;
        
        if(fl.floatValue()>0.1)
        	format="######.0000";
        else
        	format="######.0000000";
        return new DecimalFormat(format).format((Float) value);
    }
}

